﻿using System;
using System.Data.SqlClient;
using System.Collections.ObjectModel;
using System.Data;
using Eassets.Layered.Domain.Asset;


namespace Eassets.Data.Asset
{
    public class OrganizationLevelListAssetData
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="organizationLevelAsset"></param>
        public void AddOrganizationLevelListAsset(OrganizationLevelListAsset organizationLevelAsset)
        {
            SqlCommand command = new SqlCommand("SP_AddOrganizationLevelListAsset"); // Name of the SP
            CommonData commonData = new CommonData();
            commonData.ExecuteNonQuery(ConstructCommand(command, organizationLevelAsset));
        }

        /// <summary>
        /// Updates organizationLevelAsset by asset id.
        /// </summary>
        /// <param name="organizationLevelAsset"></param>
        public void ModifyOrganizationLevelListAssetByAssetId(OrganizationLevelListAsset organizationLevelAsset)
        {
            SqlCommand command = new SqlCommand("SP_ModifyOrganizationLevelListAssetByAssetId"); // Name of the SP
            //command.Parameters.AddWithValue("@OrganizationLevelListId", organizationLevelAsset.OrganizationLevelListId);
            //command.Parameters.AddWithValue("@AssetId", organizationLevelAsset.AssetId);
            //command.Parameters.AddWithValue("@NumberOfAssets", organizationLevelAsset.NoOfAssets);
            CommonData commonData = new CommonData();
            commonData.ExecuteNonQuery(ConstructCommand(command, organizationLevelAsset));
        }

        /// <summary>
        /// Updates organizationLevelAsset.
        /// </summary>
        /// <param name="organizationLevelAsset"></param>
        public void ModifyOrganizationLevelListAsset(OrganizationLevelListAsset organizationLevelAsset)
        {
            SqlCommand command = new SqlCommand("SP_ModifyOrganizationLevelListAsset"); // Name of the SP
            command.Parameters.AddWithValue("@OrganizationLevelListAssetId", organizationLevelAsset.OrganizationLevelListAssetId);
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(ConstructCommand(command, organizationLevelAsset));
        }

        /// <summary>
        /// Minus 1 organizationLevelAsset.
        /// </summary>
        /// <param name="assetId"></param>
        public void MinusOrganizationLevelListAsset(long assetId)
        {
            SqlCommand command = new SqlCommand("SP_MinusOrganizationLevelListAsset"); // Name of the SP
            command.Parameters.AddWithValue("@AssetId", assetId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(command);
        }


        //public void ModifyOrganizationLevelListAssetByAssetId(OrganizationLevelListAsset organizationLevelAsset)
        //{
        //    SqlCommand command = new SqlCommand("[SP_ModifyOrganizationLevelListAssetByAssetId]"); // Name of the SP
        //    command.Parameters.AddWithValue("@AssetId", organizationLevelAsset.AssetId);
        //     CommonData commonData = new CommonData();commonData.ExecuteNonQuery(ConstructCommand(command, organizationLevelAsset));
        //}

        /// <summary>
        /// Delete organizationLevelAsset.
        /// </summary>
        /// <param name="organizationLevelAssetId"></param>
        public void DeleteOrganizationLevelListAsset(long organizationLevelAssetId)
        {
            SqlCommand command = new SqlCommand("SP_DeleteOrganizationLevelListAsset"); // Name of the SP
            command.Parameters.AddWithValue("@OrganizationLevelListAssetId", organizationLevelAssetId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(command);
        }

        /// <summary>
        /// Get organizationLevelAsset by id.
        /// </summary>
        /// <param name="organizationLevelAssetId"></param>
        /// <returns></returns>
        public OrganizationLevelListAsset GetByIdOrganizationLevelListAsset(long organizationLevelAssetId)
        {
            SqlCommand command = new SqlCommand("SP_GetOrganizationLevelListAssetById");
            command.Parameters.AddWithValue("@OrganizationLevelListAssetId", organizationLevelAssetId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            OrganizationLevelListAsset organizationLevelAsset = new OrganizationLevelListAsset();
            while (dataReader.Read())
            {
                if (!(String.IsNullOrEmpty(dataReader["OrganizationLevelListAssetId"].ToString())))
                {
                    organizationLevelAsset.OrganizationLevelListAssetId = Convert.ToInt64(dataReader["OrganizationLevelListAssetId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["OrganizationLevelId"].ToString())))
                {
                    organizationLevelAsset.OrganizationLevelListId = Convert.ToInt64(dataReader["OrganizationLevelListId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetId"].ToString())))
                {
                    organizationLevelAsset.AssetId = Convert.ToInt64(dataReader["AssetId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["NoOfAssets"].ToString())))
                {
                    organizationLevelAsset.NoOfAssets = Convert.ToInt32(dataReader["NoOfAssets"]);
                }
            }
            dataReader.Close();
            return organizationLevelAsset;
        }

        /// <summary>
        /// Get All organizationLevelAsset.
        /// </summary>
        /// <returns></returns>
        public Collection<OrganizationLevelListAsset> GetAllOrganizationLevelListAsset()
        {
            Collection<OrganizationLevelListAsset> organizationLevelAssets = new Collection<OrganizationLevelListAsset>();
            SqlCommand command = new SqlCommand("SP_GetAllOrganizationLevelListAssets");
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            OrganizationLevelListAsset organizationLevelAsset;
            while (dataReader.Read())
            {
                organizationLevelAsset = new OrganizationLevelListAsset();
                if (!(String.IsNullOrEmpty(dataReader["OrganizationLevelListAssetId"].ToString())))
                {
                    organizationLevelAsset.OrganizationLevelListAssetId = Convert.ToInt64(dataReader["OrganizationLevelListAssetId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["OrganizationLevelId"].ToString())))
                {
                    organizationLevelAsset.OrganizationLevelListId = Convert.ToInt64(dataReader["OrganizationLevelListId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetId"].ToString())))
                {
                    organizationLevelAsset.AssetId = Convert.ToInt64(dataReader["AssetId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["NoOfAssets"].ToString())))
                {
                    organizationLevelAsset.NoOfAssets = Convert.ToInt32(dataReader["NoOfAssets"]);
                }

                organizationLevelAssets.Add(organizationLevelAsset);
            }
            dataReader.Close();
            return organizationLevelAssets;
        }

        public Collection<OrganizationLevelListAssetCount> GetAllOrganizationLevelListAssetCount()
        {
            Collection<OrganizationLevelListAssetCount> organizationLevelAssetCounts = new Collection<OrganizationLevelListAssetCount>();

            SqlCommand command = new SqlCommand("SP_GetAllOrganizationLevelListAssetCount");
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            OrganizationLevelListAssetCount organizationLevelAssetCount;
            while (dataReader.Read())
            {
                organizationLevelAssetCount = new OrganizationLevelListAssetCount();
                if (!(String.IsNullOrEmpty(dataReader["AssetId"].ToString())))
                {
                    organizationLevelAssetCount.AssetId = Convert.ToInt16(dataReader["AssetId"]);
                }

                if (!(String.IsNullOrEmpty(dataReader["code"].ToString())))
                {
                    organizationLevelAssetCount.Code = Convert.ToString(dataReader["code"]);
                }

                if (!(String.IsNullOrEmpty(dataReader["description"].ToString())))
                {
                    organizationLevelAssetCount.Description = Convert.ToString(dataReader["description"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["OPNVAL"].ToString())))
                {
                    organizationLevelAssetCount.Count = Convert.ToInt32(dataReader["OPNVAL"]);
                }


                organizationLevelAssetCounts.Add(organizationLevelAssetCount);
            }
            dataReader.Close();

            return organizationLevelAssetCounts;
        }

        public SqlCommand ConstructCommand(SqlCommand command, OrganizationLevelListAsset organizationLevelAsset)
        {
            command.CommandType = CommandType.StoredProcedure;

            try
            {
                command.Parameters.AddWithValue("@OrganizationLevelListId", organizationLevelAsset.OrganizationLevelListId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@AssetId", organizationLevelAsset.AssetId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@NoOfAssets", organizationLevelAsset.NoOfAssets);
            }
            catch (Exception)
            { }
            return command;
        }

    }
}

